<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<%@page pageEncoding="UTF-8"%>
<%@include file="../WEB-INF/jspf/controlesessao.jspf"%>

<c:catch var ="catchException">
    <fmt:parseDate var="data_alocacao" value="${param.data_alocacao}" pattern="dd/MM/yyyy"/>
    <c:choose>
        <c:when test="${param.oper=='edit'}">
            <sql:update var="update" dataSource="jdbc/dbo_MicrosoftSQLServer">
                update Alocacoes
                set  data_alocacao=?,
                hora_inicio=?,
                hora_fim=?,
                matricula=?,
                id_sala=isnull(?,id_sala),
                otimizado=?
                where id_alocacao=?
                <sql:dateParam value="${data_alocacao}"/>
                <sql:param value="${param.hora_inicio}"/>
                <sql:param value="${param.hora_fim}"/>
                <sql:param value="${param.matricula}"/>
                <sql:param value="${param.id_sala}"/>
                <sql:param value="${param.otimizado}"/>
                <sql:param value="${param.id}"/>
            </sql:update>
        </c:when>
        <c:when test="${param.oper=='add'}">
            <sql:query var="query" dataSource="jdbc/dbo_MicrosoftSQLServer">
                SELECT  * FROM reservas
                where id_reserva=?
                <sql:param value="${param.id_reserva}"/>
            </sql:query>
            <c:forEach var="reserva" items="${query.rows}">
                <c:set var="id_turma" value="${reserva.id_turma}"/>
                <c:set var="id_modulo" value="${reserva.id_modulo}"/>
                <c:set var="id_componente" value="${reserva.id_componente}"/>
            </c:forEach>
            <sql:update var="insert" dataSource="jdbc/dbo_MicrosoftSQLServer">
                insert into alocacoes (id_turma,data_alocacao,id_componente,hora_inicio,
                hora_fim,matricula,id_sala,id_modulo,carga_horaria)
                values (?,?,?,?,?,?,?,?,?)
                <sql:param value="${id_turma}"/>
                <sql:dateParam value="${data_alocacao}"/>
                <sql:param value="${id_componente}"/>
                <sql:param value="${param.hora_inicio}"/>
                <sql:param value="${param.hora_fim}"/>
                <sql:param value="${param.matricula}"/>
                <sql:param value="${param.id_sala}"/>
                <sql:param value="${id_modulo}"/>
                <sql:param value="${param.carga_horaria}"/>
            </sql:update>
        </c:when>
        <c:when test="${param.oper=='del'}">
            <sql:update var="update" dataSource="jdbc/dbo_MicrosoftSQLServer">
                delete from alocacoes
                where id_alocacao=?
                <sql:param value="${param.id}"/>
            </sql:update>
        </c:when>
    </c:choose>

</c:catch>
<c:if test = "${catchException!=null}">
    Erro : Não foi possível realizar a alteração.<br><br>
    <p  style="color:red">${fn:substringAfter(catchException,"BD:")} </p>
    <sql:update var="update" dataSource="jdbc/dbo_MicrosoftSQLServer">
        insert into testes values(?)
        <sql:param value="${fn:substring(catchException,1,1000)}"/>
    </sql:update>
</c:if>

